package com.cloud.pharmaceuticalClient.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.pharmaceuticalClient.mapper.ShoppingMapper;
import com.cloud.pharmaceuticalClient.pojo.Shopping;
import com.cloud.pharmaceuticalClient.service.ShoppingService;
import org.springframework.stereotype.Service;

/**
 * @author zhozixiong
 * @date 2023/02/25
 */
@Service
public class ShoppingServiceImpl extends ServiceImpl<ShoppingMapper, Shopping> implements ShoppingService {

    private static final int SIZE = 10;

    @Override
    public Page<Shopping> searchAllByUid(int pages, String uid) {
        Page page = new Page(pages, SIZE);
        return baseMapper.searchAllByUid(page, uid);
    }

    @Override
    public boolean insert(Shopping shopping) {
        Shopping shopping1 = baseMapper.searchAllByUidAndDid(shopping.getUid(), shopping.getDid());
        //判断该商品是否添加到购物车
        if(shopping1 == null) {
            return baseMapper.insert(shopping) >= 1;
        }else {
            return baseMapper.updateQuantityByUidAndDid(shopping) >= 1;
        }
    }

    @Override
    public boolean updateQuantity(Shopping shopping) {
        return baseMapper.updateQuantityByUidAndDid(shopping) >= 1;
    }

    @Override
    public boolean delByUid(String uid) {
        return baseMapper.delByUid(uid) >= 1;
    }

    @Override
    public boolean delByUidAndDid(String uid, Integer did) {
        return baseMapper.delByUidAndDid(uid, did) >= 1;
    }
}
